/**
 * @Author: Jackey 327156719@qq.com
 * @Date: 2025-06-26 10:33:54
 * @LastEditors: Jackey 327156719@qq.com
 * @LastEditTime: 2025-06-26 13:32:27
 * @FilePath: src/main/java/org/springblade/common/utils/ivrcall/IvrCallUtil.java
 * @Description: 这是默认设置,可以在设置》工具》File Description中进行配置
 */
package org.springblade.common.utils.ivrcall;

import com.aliyun.dyvmsapi20170525.Client;
import com.aliyun.dyvmsapi20170525.models.IvrCallRequest;
import com.aliyun.dyvmsapi20170525.models.IvrCallResponse;
import com.aliyun.teaopenapi.models.Config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class IvrCallUtil {

  private final Client ivrCallClient;
  private final ObjectMapper objectMapper; // 用于处理JSON参数

  public IvrCallUtil(
      @Value("${sms.access-key}") String accessKeyId,
      @Value("${sms.secret-key}") String accessKeySecret) throws Exception {

    Config config = new Config()
        .setAccessKeyId(accessKeyId)
        .setAccessKeySecret(accessKeySecret)
        .setEndpoint("dyvmsapi.aliyuncs.com"); // 可配置的端点

    this.ivrCallClient = new Client(config);
    this.objectMapper = new ObjectMapper();
  }

  /**
   * 发起交互式语音通话
   *
   * @param requestDto 包含IvrCall参数的DTO
   * @return IvrCallResponse 响应对象
   * @throws Exception API调用或参数处理异常
   */
  public IvrCallResponse initiateIvrCall(IvrCallRequestDto requestDto) throws Exception {
    IvrCallRequest ivrCallRequest = new IvrCallRequest();

    // 映射基本参数
    ivrCallRequest.setCalledNumber(requestDto.getCalledNumber());
    ivrCallRequest.setCalledShowNumber(requestDto.getCalledShowNumber());
    ivrCallRequest.setPlayTimes(requestDto.getPlayTimes());
    ivrCallRequest.setStartCode(requestDto.getStartCode());
	ivrCallRequest.setTimeout(requestDto.getTimeout());
//    ivrCallRequest.setByeCode(requestDto.getByeCode());
    ivrCallRequest.setOutId(requestDto.getOutId());

    // 处理JSON参数
    if (requestDto.getStartTtsParams() != null) {
      ivrCallRequest.setStartTtsParams(objectMapper.writeValueAsString(requestDto.getStartTtsParams()));
    }
    if (requestDto.getByeTtsParams() != null) {
      ivrCallRequest.setByeTtsParams(objectMapper.writeValueAsString(requestDto.getByeTtsParams()));
    }
    if (requestDto.getMenuKeyMap() != null && !requestDto.getMenuKeyMap().isEmpty()) {
      ivrCallRequest.setMenuKeyMap(requestDto.getMenuKeyMap());
    }

    // 发起同步API调用
    return ivrCallClient.ivrCall(ivrCallRequest);
  }

  // 内部 DTO 类定义示例
  public static class IvrCallRequestDto {
    private String calledNumber;
    private String calledShowNumber = "057123676193";
    private Long playTimes;
    private String startCode;
    private Object startTtsParams; // 使用Object或Map来表示JSON结构
    private String byeCode;
    private Object byeTtsParams;
    private java.util.List<IvrCallRequest.IvrCallRequestMenuKeyMap> menuKeyMap; // 使用List来表示JSON数组
    private String outId;

	private Integer timeout; // 等待用户按键超时时间。单位：毫秒。

    // Getters and Setters
    public String getCalledNumber() {
      return calledNumber;
    }

    public void setCalledNumber(String calledNumber) {
      this.calledNumber = calledNumber;
    }

    public String getCalledShowNumber() {
      return calledShowNumber;
    }

    public void setCalledShowNumber(String calledShowNumber) {
      this.calledShowNumber = calledShowNumber;
    }

    public Long getPlayTimes() {
      return playTimes;
    }

    public void setPlayTimes(Long playTimes) {
      this.playTimes = playTimes;
    }

    public String getStartCode() {
      return startCode;
    }

    public void setStartCode(String startCode) {
      this.startCode = startCode;
    }

    public Object getStartTtsParams() {
      return startTtsParams;
    }

    public void setStartTtsParams(Object startTtsParams) {
      this.startTtsParams = startTtsParams;
    }

    public String getByeCode() {
      return byeCode;
    }

    public void setByeCode(String byeCode) {
      this.byeCode = byeCode;
    }

    public Object getByeTtsParams() {
      return byeTtsParams;
    }

    public void setByeTtsParams(Object byeTtsParams) {
      this.byeTtsParams = byeTtsParams;
    }

    public List<IvrCallRequest.IvrCallRequestMenuKeyMap> getMenuKeyMap() {
      return menuKeyMap;
    }

    public void setMenuKeyMap(java.util.List<IvrCallRequest.IvrCallRequestMenuKeyMap> menuKeyMap) {
      this.menuKeyMap = menuKeyMap;
    }

    public String getOutId() {
      return outId;
    }

    public void setOutId(String outId) {
      this.outId = outId;
    }

	  public Integer getTimeout() {
		  return timeout;
	  }

	  public void setTimeout(Integer timeout) {
		  this.timeout = timeout;
	  }
  }

}
